package com.cskaoyan.service;


import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketAddress;
import com.cskaoyan.bean.marketBean.MarketAddressExample;
import com.cskaoyan.bean.marketBean.MarketSearchHistory;
import com.cskaoyan.bean.marketBean.MarketSearchHistoryExample;

import com.cskaoyan.mapper.marketMapper.MarketAddressMapper;
import com.cskaoyan.mapper.marketMapper.MarketSearchHistoryMapper;

import cn.hutool.core.bean.BeanUtil;
import com.cskaoyan.bean.common.BaseParam;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketFeedback;
import com.cskaoyan.bean.marketBean.MarketFeedbackExample;



import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.marketBean.MarketUserExample;

import com.cskaoyan.bean.vo.zy.ZYAdminUserListVO;

import com.cskaoyan.mapper.marketMapper.HomeMapper_yjc;
import com.cskaoyan.mapper.marketMapper.MarketFeedbackMapper;

import com.cskaoyan.mapper.marketMapper.MarketUserMapper;

import com.cskaoyan.mapper.marketMapper.UserMapper_yjc;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceimpl_yjc implements UserService_yjc {


    @Autowired
    UserMapper_yjc userMapper_yjc;
    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketSearchHistoryMapper historyMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackrMapper;


    @Override
    public List<MarketFeedback> selectByExample(MarketFeedbackExample example) {
        return marketFeedbackrMapper.selectByExample(example);
    }


    @Override
    public PageInfo queryAllUsers() {
        PageHelper.startPage(1, 20);
        List list = userMapper_yjc.queryAllUsers();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public CommonData queryAllAddress(BasePageInfo info, String name, Integer userId) {
        PageHelper.startPage(1, 20);
        MarketAddressExample example = new MarketAddressExample();
        //设置去重
        example.setDistinct(true);
        //设置排序规则，严格按照格式进行书写（前面是排序的colum，后面是指升序还是降序）
        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        //使用example对象创建criteria对象
        MarketAddressExample.Criteria criteria = example.createCriteria();
        //先对条件进行判空，不为空的情况下，使用criteria中的方法进行where条件的添加
        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name +"%");
        }
        if (userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        //将example对象传入sql中获取list集合
        List<MarketAddress> marketAddresseList = addressMapper.selectByExample(example);
        //new一个PageInfo对象，将list集合传进去
        PageInfo<MarketAddress> addressInfo = new PageInfo<>(marketAddresseList);
        //返回
        return CommonData.data(addressInfo);

    }

    @Override
    public PageInfo queryCollect() {
        PageHelper.startPage(1, 20);
        List list = userMapper_yjc.queryCollect();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public PageInfo queryFootprint() {
        PageHelper.startPage(1, 20);
        List list = userMapper_yjc.queryFootprint();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public CommonData queryHistory(BasePageInfo info, String keyword, Integer userId) {

        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        //设置去重
        example.setDistinct(true);
        //设置排序规则，严格按照格式进行书写（前面是排序的colum，后面是指升序还是降序）
        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        //使用example对象创建criteria对象
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        //先对条件进行判空，不为空的情况下，使用criteria中的方法进行where条件的添加
        if (keyword != null && !"".equals(keyword)){
            criteria.andKeywordLike("%" + keyword +"%");
        }
        if (userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        //将example对象传入sql中获取list集合
        List<MarketSearchHistory> historyList = historyMapper.selectByExample(example);
        //new一个PageInfo对象，将list集合传进去
        PageInfo<MarketSearchHistory> historyInfo = new PageInfo<>(historyList);
        //返回
        return CommonData.data(historyInfo);
    }

    @Override
    public PageInfo queryFeedback() {
        PageHelper.startPage(1, 20);
        List list = userMapper_yjc.queryFeedback();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public CommonData<ZYAdminUserListVO> userList(BaseParam param, String username, String mobile, Integer userId) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();

        userExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        List<MarketUser> resultList = marketUserMapper.selectByExample(userExample);
        List<ZYAdminUserListVO> userListVO = new ArrayList<>();
        for (MarketUser user : resultList) {
            ZYAdminUserListVO userVO = new ZYAdminUserListVO();
            String[] ignoreProperties = {"birthday"};
            BeanUtil.copyProperties(user, userVO, ignoreProperties);
            userListVO.add(userVO);
        }

        PageInfo<ZYAdminUserListVO> result = new PageInfo<>(userListVO);
        CommonData data = CommonData.data(result);
        return data;
    }

    @Override
    public ZYAdminUserListVO uderDetail(Integer id) {
        MarketUser user = marketUserMapper.selectByPrimaryKey(id);
        ZYAdminUserListVO resultVO = new ZYAdminUserListVO();
        String[] ignoreProperties = {"birthday"};
        BeanUtil.copyProperties(user,resultVO,ignoreProperties);

        return resultVO;
    }

    @Override
    public int userUpdate(ZYAdminUserListVO param) {
        MarketUser user = new MarketUser();
        BeanUtil.copyProperties(param,user);
        int affctedRow = marketUserMapper.updateByPrimaryKeySelective(user);
        return affctedRow;
    }
}
